﻿using System;
using System.Net;
using System.IO;
using System.Windows.Forms;

namespace TackMessenger
{
    public partial class Form1 : Form
    {
        public static string nickName;


        public Form1()
        {
            InitializeComponent();
            this.KeyPreview = true;

            string myIP = getMyIp();

            extIP.Text = myIP;


            //IPHostEntry IPHost = Dns.GetHostEntry(Dns.GetHostName());
            //extIP.Text = IPHost.AddressList[0].ToString();

            }


        private string getMyIp()
        {
            String ipAddress = "";
            String result = "";

            WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
            request.Method = "POST";
            request.Credentials = CredentialCache.DefaultCredentials;
            Stream dataStream = request.GetRequestStream();
            WebResponse response = request.GetResponse();
            PartyPlace.AppendText("--- Attempting to acquire public IP Address... ");
            PartyPlace.AppendText(((HttpWebResponse)response).StatusDescription + "\n");
            dataStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(dataStream);
            ipAddress = reader.ReadToEnd();
            
            Char[] address = ipAddress.ToCharArray();
            Char ch;
            for (int i = 0; i < ipAddress.Length; ++i)
            {
                ch = address[i];
                if (ch == '.' || Char.IsNumber(ch))
                {
                    result += address[i];
                }

                

            }

            response.Close();
            reader.Close();
            if (result.Length == 0)
            {
                result = "unkown";
            }

            return result;
        }


        private void Send_Click(object sender, EventArgs e)
        {
            if (TextArea.Text != "")
            {
                PartyPlace.AppendText("     " + Props.getUsername() + " says: \n" + TextArea.Text + "\n\n");
                TextArea.Text = "";
                TextArea.Focus();
            }
        }

        private void setNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NameGetter name = new NameGetter();
            
            
            name.Show();

            
        }

        

        private void TextArea_TextChanged(object sender, EventArgs e)
        {

            this.KeyDown += new KeyEventHandler(Form1_KeyDown);


        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter && !e.Shift)
            {
                e.SuppressKeyPress = true;
                if (TextArea.Text != "")
                {
                    PartyPlace.AppendText("    " + Props.getUsername() + " says: \n" + TextArea.Text + "\n\n");
                    TextArea.Text = "";
                }


            }


        }

        private void connectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Connection connect = new Connection();

            connect.Show();
        }
      

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {
            
                
        }

        




    }
}
